﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using CommonUtility;
using DAL;
using System.Data.SqlClient;

namespace BLL
{
    public class bllLog
    {
        /// <summary>
        /// 记录操作日志
        /// </summary>
        /// <param name="_funID">功能ID</param>
        /// <param name="_actName">操作名称</param>
        /// <param name="_actDesc">操作说明</param>
        /// <param name="_whom">操作人</param>
        /// <returns></returns>
        public static bool AddLog(string _funID, string _actName, string _actDesc, string _whom)
        {
            LogInfo log = new LogInfo();
            log.FunID = _funID;
            log.ActName = _actName;
            log.ActDesc = _actDesc;
            log.Whom = _whom;
            return Log.Add(log);
        }

        /// <summary>
        /// 记录操作日志
        /// </summary>
        /// <param name="_funID">功能ID</param>
        /// <param name="_actName">操作名称</param>
        /// <param name="_actDesc">操作说明</param>
        /// <param name="_whom">操作人</param>
        ///  <param name="_myTran">事务</param>
        /// <returns></returns>
        public static bool AddLog(string _funID, string _actName, string _actDesc, string _whom, SqlTransaction _myTran)
        {
            LogInfo log = new LogInfo();
            log.FunID = _funID;
            log.ActName = _actName;
            log.ActDesc = _actDesc;
            log.Whom = _whom;
            return Log.Add(log, _myTran);
        }


        /// <summary>
        /// 获取数据，条件带参数
        /// </summary>
        /// <param name="_strFromTime">开始时间</param>
        /// <param name="_strToTime">结束时间</param>
        /// <param name="_strWhom">用户名</param>
        /// <param name="_strActionName">功能描述</param>
        /// <param name="_condition">筛选条件</param>
        /// <param name="_strOrder">排序方式</param>
        /// <param name="_pageIndex">页码，从1开始</param>
        /// <param name="_pageSize">页面记录数</param>
        /// <param name="_total">返回总记录数</param>
        /// <returns>当前页记录</returns>
        public static IList<LogInfo> GetList(string _strFromTime, string _strToTime, string _strWhom, string _strFunID, string _strActionName, string _strOrder, int _pageIndex, int _pageSize, ref int _total)
        {
            string message = string.Empty;
            string strSQL = " 1=1 ";
            string strFromTime = Common.ConvertSql(_strFromTime);
            string strToTime = Common.ConvertSql(_strToTime);
            string strWhom = Common.ConvertSql(_strWhom);
            string strFunID = Common.ConvertSql(_strFunID);
            string strActionName = Common.ConvertSql(_strActionName);

            if (!string.IsNullOrEmpty(strFromTime) && string.IsNullOrEmpty(strToTime))
            {
                strSQL += " and Lpt >= '" + strFromTime + " 00:00:00' ";
            }
            else if (string.IsNullOrEmpty(strFromTime) && !string.IsNullOrEmpty(strToTime))
            {
                strSQL += " and Lpt <= '" + strToTime + " 23:59:59' ";
            }
            else if (!string.IsNullOrEmpty(strFromTime) && !string.IsNullOrEmpty(strToTime))
            {
                strSQL += " and Lpt >= '" + strFromTime + " 00:00:00' and Lpt <= '" + strToTime + " 23:59:59' ";
            }

            if (!string.IsNullOrEmpty(strWhom))
            {
                strSQL += " and Whom like '%" + strWhom + "%' ";
            }

            if (!string.IsNullOrEmpty(strFunID))
            {
                strSQL += " and FunID = '" + strFunID + "' ";
            }

            if (!string.IsNullOrEmpty(strActionName))
            {
                strSQL += " and ActName like '%" + strActionName + "%' ";
            }

            IList<LogInfo> list = Log.GetPageObject(_pageIndex, _pageSize, strSQL, _strOrder.Trim(), ref _total, ref message);
            foreach (LogInfo newList in list)
            {
                //string desc = bllAction.GetActDesc(newList.ActName);
                //newList.ActName = string.IsNullOrEmpty(desc) ? newList.ActName : desc;
                newList.FunID = bllFunction.GetFunDesc(newList.FunID);
            }
            return list;
        }
    }
}
